//
//  DHS2_BrandViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/7/7.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_BrandViewController.h"
#import "DHS2_Brand_HeadCell.h"
#import "DHS2_Brand_ContentCell.h"

@interface DHS2_BrandViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSString *brandID;

@property (assign, nonatomic) NSInteger pageIndex;
@property (strong, nonatomic) NSMutableArray *dataArray;
@property (strong, nonatomic) NSDictionary *brandInfo;

@property (strong, nonatomic) NSString *show_control;

@property (strong, nonatomic) NSNumber *webHeight;

@end

@implementation DHS2_BrandViewController

- (instancetype)initWithBrandID:(NSString *)brandId
{
    self = [super init];
    if (self) {
        self.brandID = brandId;
    }
    return self;
}


- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.show_control = @"sp_action";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_Brand_HeadCell" bundle:nil] forCellReuseIdentifier:kDHS2_Brand_HeadCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_Brand_ContentCell" bundle:nil] forCellReuseIdentifier:kDHS2_Brand_ContentCell];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreProduct)];
    
    [self loadInfo];
    [self loadProduct];
}

- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    if ([eventName isEqualToString:@"sp_action"]) {
        self.show_control = @"sp_action";
        [self.tableView reloadRow:1 inSection:0 withRowAnimation:UITableViewRowAnimationNone];
    }else if ([eventName isEqualToString:@"xx_action"]){
        self.show_control = @"xx_action";
        [self.tableView reloadRow:1 inSection:0 withRowAnimation:UITableViewRowAnimationNone];
    }else if ([eventName isEqualToString:@"brand_info_webViewDidFinishLoad"]) {
        self.webHeight = [userInfo objectForKey:@"height"];
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
    }
}

- (void)loadProduct{
    self.pageIndex = 0;
    NSString *sex = [GlobalManager shareGlobalManager].user.sex? : [[NSUserDefaults standardUserDefaults] objectForKey:USER_SELECT_SEX];
    [[DHNetAPIClient shareDHNetAPIClient] request_productsWithParams:@{@"brand_id":self.brandID,@"pageIndex":@(self.pageIndex),@"pageSize":@(20)} andBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:[data objectForKey:@"products"]];
        if (self.dataArray.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        self.pageIndex += 1;
        [self.tableView reloadData];
    }];
}

- (void)loadMoreProduct{
    NSString *sex = [GlobalManager shareGlobalManager].user.sex? : [[NSUserDefaults standardUserDefaults] objectForKey:USER_SELECT_SEX];
    [[DHNetAPIClient shareDHNetAPIClient] request_productsWithParams:@{@"brand_id":self.brandID,@"pageIndex":@(self.pageIndex),@"pageSize":@(20)} andBlock:^(id data, NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [self.dataArray addObjectsFromArray:[data objectForKey:@"products"]];
        if (self.dataArray.count < 20 * (self.pageIndex + 1)) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        self.pageIndex += 1;
        [self.tableView reloadData];
    }];
}

- (void)loadInfo{
    [[DHNetAPIClient shareDHNetAPIClient] request_brands_detailWithParams:@{@"brand_id":self.brandID} AndBlock:^(id data, NSError *error) {
        self.brandInfo = [data objectForKey:@"brand"];
        self.titleLabel.text = [self.brandInfo objectForKey:@"name"];
        [self.tableView reloadData];
    }];
}

#pragma mark - tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 215;
    }else{
        if ([self.show_control isEqualToString:@"sp_action"]) {
            CGFloat h = 200;
            if (self.dataArray.count % 3 == 0) {
                return (self.dataArray.count / 3) * (h+5);
            }else{
                return (self.dataArray.count / 3 + 1) * (h+5);
            }
        }else{
            return [self.webHeight floatValue];;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        DHS2_Brand_HeadCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_Brand_HeadCell forIndexPath:indexPath];
        cell.brandID = self.brandID;
        [cell updateCell:self.brandInfo];
        return cell;
    }else{
        DHS2_Brand_ContentCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_Brand_ContentCell forIndexPath:indexPath];
        if ([self.show_control isEqualToString:@"sp_action"]) {
            cell.collectionView.hidden = NO;
            cell.webView.hidden = YES;
        }else{
            cell.collectionView.hidden = YES;
            cell.webView.hidden = NO;
        }
        [cell updateCellArr:self.dataArray];
        [cell.webView loadHTMLString:[self.brandInfo objectForKey:@"description"] baseURL:nil];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
